﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk;
using CrmCodeGenerator.Core;

namespace CrmCodeGenerator.UI
{
    public partial class Connection : Form
    {
        public Connection()
        {
            InitializeComponent();
        }

        #region multi-threading stuff
        private void UpdateStatus(string msg)
        {
            if (this.statusStrip1.InvokeRequired)
            {
                this.statusStrip1.BeginInvoke(new MethodInvoker(delegate()
                {
                    this.statusmsg.Text = msg;
                }));
            }
            else { this.statusmsg.Text = msg; }
        }

        private void ToggleControl(Control c, bool enabled)
        {
            if (c.InvokeRequired)
            {
                c.BeginInvoke(new MethodInvoker(delegate()
                {
                    c.Enabled = enabled;
                }));
            }
            else { c.Enabled = enabled; }
        }
        #endregion

        private delegate IOrganizationService GetCrmConnectionHandler(string url, string org, string domain, string username, string password);

        private void connect_Click(object sender, EventArgs e)
        {
            ToggleControl(this.connect, false);
            UpdateStatus("Connecting...");

            GetCrmConnectionHandler gcch = new GetCrmConnectionHandler(BeginGetCrmConnection);
            AsyncCallback callback = new AsyncCallback(EndGetCrmConnection);

            gcch.BeginInvoke(this.discourl.Text, this.orguniquename.Text, this.domain.Text, this.username.Text, this.password.Text, callback, gcch);
        }

        private IOrganizationService BeginGetCrmConnection(string url, string org, string domain, string username, string password)
        {
            IOrganizationService sdk = null;

            try
            {
                sdk = QuickConnection.Connect(url, domain, username, password, org);
            }
            catch (Exception ex)
            {
                UpdateStatus(string.Format("Connection failed: {0}", ex.Message));
            }

            return sdk;
        }

        private void EndGetCrmConnection(IAsyncResult result)
        {
            GetCrmConnectionHandler gcch = result.AsyncState as GetCrmConnectionHandler;
            IOrganizationService sdk = gcch.EndInvoke(result);

            ToggleControl(this.connect, true);

            if (sdk != null) 
            {
                UpdateStatus("Connected");

                Options o = new Options { Connection = sdk };
                if (this.InvokeRequired)
                {
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        o.ShowDialog();
                    }));
                }
                else
                {
                    o.ShowDialog();
                }
            }
        }
    }
}
